<?php

class Zhishidi_Express_Adminhtml_Zhishidi_Express_MapController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_express')->__('Manage Express Zone Map'));
        $this->renderLayout();
    }

    public function gridAction() {
        $this->loadLayout();
        $this->getResponse()->setBody($this->getLayout()->createBlock('zhishidi_express/adminhtml_map_grid')->toHtml());
    }

    public function editAction() {
        $id = $this->getRequest()->getParam('id');

        $model = Mage::getModel('zhishidi_express/map')->load($id);

        if ($model->getId() || $id == 0) {
            $data = Mage::getSingleton('adminhtml/session')->getExpressZoneData(true);
            if (!empty($data)) {
                $model->setData($data);
            }
            Mage::register('express_map_data', $model);
            $this->loadLayout();
            $this->_setActiveMenu('system/zhishidi_express');
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_express')->__('Manage Express Zone Map'));
            $this->_addContent($this->getLayout()->createBlock('zhishidi_express/adminhtml_map_edit'))->_addLeft($this->getLayout()->createBlock('zhishidi_express/adminhtml_map_edit_tabs'));
            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_express')->__('Express Map does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function newAction() {
        $this->_forward('edit');
    }

    public function saveAction() {
        if ($data = $this->getRequest()->getPost()) {
            try {

                $model = Mage::getModel('zhishidi_express/map');
                $model->setData($data)->setId($this->getRequest()->getParam('id'));
                $model->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('zhishidi_express')->__('Map was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setExpressMapData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return true;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setExpressMapData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return false;
            }
        }
    }

    public function massDeleteAction() {
        $massIds = $this->getRequest()->getParam('mass_action_ids');

        if (!is_array($massIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_express')->__('Please select map'));
        } else {
            try {
                foreach ($massIds as $model) {
                    $model = Mage::getModel('zhishidi_express/map')->load($model);
                    $model->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_express')->__(
                                'Total of %d Map(s) were successfully deleted', count($massIds)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction() {
        if ($this->getRequest()->getParam('id') > 0) {
            try {
                $model = Mage::getModel('zhishidi_express/map');

                $model->setId($this->getRequest()->getParam('id'))
                        ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Map was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

}

